package com.hp.windows.listener;

import com.teamdev.jxbrowser.chromium.Browser;
import com.teamdev.jxbrowser.chromium.dom.By;
import com.teamdev.jxbrowser.chromium.dom.DOMDocument;
import com.teamdev.jxbrowser.chromium.dom.DOMElement;
import com.teamdev.jxbrowser.chromium.events.*;

import java.util.List;

/**
 * 监听页面加载
 */
public class MyLoadListener implements LoadListener {

    private Browser donwBrowser;

    public MyLoadListener(Browser donwBrowser){
        this.donwBrowser=donwBrowser;
    }

    public void onStartLoadingFrame(StartLoadingEvent startLoadingEvent) {

    }

    public void onProvisionalLoadingFrame(ProvisionalLoadingEvent provisionalLoadingEvent) {

    }

    /**
     * 当页面加载完成
     * @param finishLoadingEvent
     */
    public void onFinishLoadingFrame(FinishLoadingEvent finishLoadingEvent) {
        DOMDocument document = donwBrowser.getDocument();
        List<DOMElement> list = document.findElements(By.tagName("a"));
        DOMElement d =null;
        for (int i = 0; i < list.size(); i++) {
            d = list.get(i);
            d.removeAttribute("target");
        }

    }

    public void onFailLoadingFrame(FailLoadingEvent failLoadingEvent) {

    }

    public void onDocumentLoadedInFrame(FrameLoadEvent frameLoadEvent) {

    }

    public void onDocumentLoadedInMainFrame(LoadEvent loadEvent) {

    }
}
